"""CNN加载"""
from keras.models import load_model
from tensorflow.examples.tutorials.mnist import input_data
import matplotlib.pyplot as plt
import numpy as np

#模型加载
model_save_path="./CNN.h5"
model=load_model(model_save_path)

img_size=28
mnist=input_data.read_data_sets("./data",one_hot=True)
X_train,y_train,X_test,y_test=mnist.train.images,mnist.train.labels,mnist.test.images,mnist.test.labels
X_train=X_train.reshape(-1,img_size,img_size,1)
X_test=X_test.reshape(-1,img_size,img_size,1)
print(X_train[0].shape)
print(y_train[0].shape)
X_train=X_train.reshape(-1,img_size,img_size)
X_test=X_test.reshape(-1,img_size,img_size)
X_train=X_train*255
X_test=X_test*255

sample=X_test[0]
print(sample.shape) #(28,28)
plt.imshow(sample, cmap=plt.get_cmap('gray'))
plt.show()

#模型验证
#图片数量,pixel2dim,通道
sample=sample.reshape(1,sample.shape[0],sample.shape[1],1)
print(sample.shape) #(1,28, 28, 1)
sample=sample/255

#预测
pred=model.predict(sample)
#输出为(1,10)
print(pred.shape)
predict = np.argmax(pred,axis=1) #axis = 1是取行的最大值的索引，0是列的最大值的索引
print(predict)